package kr.hkit.android_bookmgr.adapter;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import kr.hkit.android_bookmgr.R;
import kr.hkit.android_bookmgr.DTO.BookData;
import kr.hkit.android_bookmgr.util.ImageUtil;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainListAdapter extends BaseAdapter { // booklist 나오게 - 사용!!
	
	private LayoutInflater mInflater;
	private ArrayList<BookData> arItems = new ArrayList<BookData>();
	private View v;
	private ContactViewHolder holder;
	private Context context;
	
	public MainListAdapter(Context context, ArrayList<BookData> arItems) {
		this.context = context;
		this.mInflater = LayoutInflater.from(context); 
		this.arItems = arItems;
		
	}
	
	static class ContactViewHolder{
		ImageView mImg;
		TextView mName;
		TextView mProgress;
		ImageView pangimg;
		}

	@Override
	public int getCount() {
		return arItems.size();
	}

	@Override
	public Object getItem(int position) {
		return arItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		v = convertView;
		
		if(v==null){
			v = mInflater.inflate(R.layout.booklist_grid_row, null);
			
			holder = new ContactViewHolder();
			holder.mImg = (ImageView) v.findViewById(R.id.booklist_img);
			holder.mName = (TextView) v.findViewById(R.id.booklist_name);
			holder.mProgress = (TextView) v.findViewById(R.id.booklist_progress);
			holder.pangimg = (ImageView) v.findViewById(R.id.booklist_pangimg);
			
			v.setTag(holder);
		}else {
			holder = (ContactViewHolder) v.getTag();
		}
		
		BookData c = arItems.get(position);
		
		ImageUtil.imageLoader(context);
		DisplayImageOptions options = ImageUtil.dispalyOptions();
		
		//이미지 불러오기
		String str = c.getBookimg(); 
		int imgtype = c.getImgtype();
		if (imgtype==0){ //직찍
			ImageLoader.getInstance().displayImage("file:///"+str, holder.mImg, options);
		}else if(imgtype==1){ //갤러리
			ImageLoader.getInstance().displayImage("content://media/"+str, holder.mImg, options);
		}else{ //검색
			ImageLoader.getInstance().displayImage(str, holder.mImg, options);
		}
		
		//책제목
		holder.mName.setText(c.getName());
		
		//진행률
		int result = c.getProgress();
		if(result>0 && result<100){
			holder.mProgress.setText(result+"%");
		}else if (result==100){
			holder.mProgress.setText(result+"%");
			holder.pangimg.setVisibility(ImageView.VISIBLE);
		}
		
		return v;
	}
	
}
